/**
 * Created by chengming on 18/4/18.
 * version 1.0
 */
const BaseAdapter = require('./base/BaseAdapter');
const BaseHrefAdapter = require('./base/BaseHrefAdapter')
const logger = require('../conf/logger');
const request = require('request-promise');
const moment = require('moment');
const urlTool = require('url');
const iconv = require("iconv-lite");
const report = require('../exception/ErrorReport');

const cheerio =  require('cheerio');

class ArticleHrefAdapter extends BaseHrefAdapter{

	constructor(page, isTest){
		super(page, isTest);
	}

	parseHtml(html, linkRule){
		//将解析后的内容加载进解析器
		let $ = cheerio.load(html);

		let contents = [];
		let that = this;
		let rangeText = '';


		$(this.linkRule).each( function() {

			let title = $(this).text().trim();
			let href = '';
			if($(this).is('a') && $(this).attr('href')){
				href = urlTool.resolve(that.requestUrl, $(this).attr('href'));
			}

			if(title.length != 0 || href.length != 0){
				contents.push({
					title:title,
					href:href
				});
				rangeText +=title+href;
			}
		});

		return {
			rangeText: rangeText,
			contents: contents
		};
	}
}

module.exports = ArticleHrefAdapter;